<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :inline="true">
      <el-form-item label="版本名称">
        <el-input v-model="q" placeholder="请输入版本名称" clearable @keyup.enter.native="fetchData" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
        <el-button type="primary" @click="addVersion">新增版本</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="versionList"
      border
      :header-cell-style="{ background: '#fafafa' }"
    >
      <el-table-column prop="name" label="版本名称" />
      <el-table-column prop="start_time" label="开始时间" />
      <el-table-column prop="end_time" label="结束时间" />
      <el-table-column prop="order" label="排序" />
      <el-table-column prop="desc" label="描述" />
      <el-table-column prop="disabled" label="是否禁用" width="100" align="center">
        <template slot-scope="scope">
          <el-switch :value="scope.row.disabled?false:true" disabled />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="editVersion(scope.row)">编辑</el-button>
          <el-button type="text" @click="deleteVersion(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="page" :limit.sync="limit" @pagination="fetchData" />

    <el-dialog :title="versionDialogTitle" :visible.sync="versionDialogShow" width="30%">
      <el-form ref="versionForm" :model="versionForm" :rules="versionFormRules" label-width="90px">
        <el-form-item label="版本名称" prop="name">
          <el-input v-model="versionForm.name" placeholder="请输入版本名称" />
        </el-form-item>
        <el-form-item label="起止时间" prop="duration">
          <el-date-picker
            v-model="versionForm.duration"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="版本序号" prop="order">
          <el-input-number v-model="versionForm.order" :min="1" :max="9999" label="排序" />
        </el-form-item>
        <el-form-item label="是否禁用" prop="disabled">
          <el-radio-group v-model="versionForm.disabled">
            <el-radio :label="false">启用</el-radio>
            <el-radio :label="true">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="versionForm.desc" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="versionDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="saveSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import apiVer from '@/api/system/version'
import Pagination from '@/components/Pagination'
const defaultForm = {
  id: '',
  name: '',
  order: '',
  start_time: '',
  end_time: '',
  disabled: false,
  duration: '',
  desc: ''
}

export default {
  components: { Pagination },
  data() {
    return {
      versionList: [],
      listLoading: false,
      page: 1,
      limit: 10,
      total: 0,
      q: '',
      versionDialogShow: false,
      versionDialogTitle: '新增版本',
      submitType: 0, // 0:新增 1:编辑
      versionForm: Object.assign({}, defaultForm),
      versionFormRules: {
        name: [
          { required: true, message: '版本名称不能为空', trigger: 'blur' }
        ],
        duration: [
          { required: true, message: '起止时间不能为空', trigger: 'blur' }
        ],
        order: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const params = {
        page: this.page,
        limit: this.limit,
        name: this.q
      }
      apiVer.getVersionList(params).then(response => {
        this.versionList = response.data
        this.total = response.count
      })
      this.listLoading = false
    },
    addVersion() {
      this.versionDialogShow = true
      this.versionDialogTitle = '新增版本'
      this.submitType = 0
      this.versionForm = Object.assign({}, defaultForm)
    },
    editVersion(row) {
      this.versionDialogShow = true
      this.versionDrawerTitle = '编辑版本'
      this.submitType = 1
      this.versionForm = Object.assign({}, row, {
        duration: !row.start_time && !row.end_time ? [] : [row.start_time, row.end_time]
      })
    },
    async saveSubmit() {
      await this.$refs.versionForm.validate(async validate => {
        if (validate) {
          this.versionForm.start_time = this.versionForm.duration[0]
          this.versionForm.end_time = this.versionForm.duration[1]
          if (this.submitType === 0) {
            await apiVer.createVersion(this.versionForm).then(response => {
              this.$message.success(response.message || '新增成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          } else {
            await apiVer.updateVersion(this.versionForm).then(response => {
              this.$message.success(response.message || '编辑成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          }
          this.versionDialogShow = false
          await this.fetchData()
        }
      })
    },
    async deleteVersion(row) {
      const data = [row.id]
      await this.deleteSubmit(data)
    },
    async deleteSubmit(data) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        lockScroll: false,
        type: 'warning'
      }).then(async() => {
        await apiVer.deleteVersion(data).then(async response => {
          this.$message.success(response.message || '删除成功')
          await this.fetchData()
        })
      })
    }
  }
}
</script>
